﻿using System;
using System.Collections.Generic;
using ECSBase.Component.MaskRelated;

namespace ECSBase
{
    public class Entity
    {
        #region Identifier

        private int _id;
        public int EntityID
        {
            get
            {
                return _id;
            }
            internal set
            {
                _id = value;
                int _componentCount = ComponentCount;
                for (int i = 0; i < _componentCount; i++)
                {
                    components[i].EntityID = _id;
                }//更新组件的id
            }
        }

        private int _mask = 0;

        public int Mask { get { return _mask; } }
        #endregion

        #region private fileds

        private int ComponentCount { get { return components.Count; } }

        #endregion

        #region Components Related

        private List<ComponentBase> components = new List<ComponentBase>();

        /// <summary>
        /// 添加一个类型是T的组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public T AddComponent<T>() where T:ComponentBase
        {
            Type type = typeof(T);
            T newComponent = type.Assembly.CreateInstance(type.FullName) as T;
            newComponent.EntityID = EntityID;
            if (this.GetComponent<T>() != null)
            {
                throw new Exception("每个实体只能拥有一个同类型组件！");
            }
            this.components.Add(newComponent);

            this._mask |= MaskDistributer.DefaultContainer.GetMaskOfType(type);

            return this.GetComponent<T>();
        }

        /// <summary>
        /// 获取第一个类型是T的组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetComponent<T>() where T : ComponentBase
        {
            int _count = this.ComponentCount;
            for (int i = 0; i < _count; i++)
            {
                if (this.components[i].GetType() == typeof(T))
                {
                    return components[i] as T;
                }
            }

            return null;
        }

        ///// <summary>
        ///// 获取所有的T类型组件（返回一个T类型的数组）
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <returns></returns>
        //public T[] GetComponents<T>() where T : ComponentBase
        //{
        //    List<T> _resList = new List<T>();

        //    int _count = this.ComponentCount;
        //    for (int i = 0; i < _count; i++)
        //    {
        //        if (this.components[i].GetType() == typeof(T))
        //        {
        //            _resList.Add(components[i] as T);
        //        }
        //    }

        //    return _resList.ToArray();
        //}//不允许一个Entity有两个以上的同类Component

        /// <summary>
        /// 删除T类型的组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void RemoveComponent<T>() where T : ComponentBase
        {
            int _count = this.ComponentCount;
            for (int i = 0; i < _count; i++)
            {
                if (this.components[i].GetType() == typeof(T))
                {
                    components.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 清空所有的组件
        /// </summary>
        public void Clear()
        {
            this._mask = MaskDistributer.DefaultContainer.None;
            components.Clear();
        }

        #endregion
    }
}
